package com.hzzh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzzh.dao.EmployeeDAO;
import com.hzzh.vo.Employee;

/**
 * Servlet implementation class ValidateUsernameServlet
 */
@WebServlet("/ValidateUsernameServlet")
public class ValidateUsernameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidateUsernameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean flag=true;
		String message="";
		EmployeeDAO dao=new EmployeeDAO();
		Employee e;
		try {
			e = dao.selectByUsername(request.getParameter("username"));
			if(e==null){
				message="用户名可以使用";
			}else{
				flag=false;
				message="用户名已存在，请选择使用其他用户名";
			}
			response.setContentType("text/xml;charset=gb2312");
			PrintWriter out=response.getWriter();
			response.setHeader("Cache-Control", "no-cache");
			out.println("<?xml version='1.0' encoding='"+"gb2312"+"'?>");
			out.println("<response>");
			out.println("<passed>"+Boolean.toString(flag)+"</passed>");
			out.println("<message>"+message+"</message>");
			out.println("</response>");
			out.close();
		
		}
		catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

}
